(ns home
  (:use util.os
        util.file
        userdict)
  (:require util.mekit))

(defn user-home []
  (System/getProperty "user.home"))

(defn appdata-home []
  (if (windows?)
    (System/getenv "APPDATA")
    (user-home)))

(def mekit-home util.mekit/mekit-home)


(def mekit-data-home-name ".mekit.data")
(def default-mekit-data-home (file-join (mekit-home) mekit-data-home-name))

;
; mekit-home/.mekit-data
; user-home/.mekit-data
; appdata/.mekit-data
; .

(defn find-existing-mekit-data []
  (first
    (filter file-exists?
      (concat
        (map #(file-join % mekit-data-home-name)
          ["." (mekit-home) (user-home)])
        [(file-join (appdata-home) "M3OTestingToolkit")]))))


(defn mekit-data-home [] ; doesn't create folder
  (or (uvar "mekit.data")
    (find-existing-mekit-data)
    default-mekit-data-home))

(defn mekit-app-home [app] ; create folder!
  (let [home (file-join (mekit-data-home) app)]
    (mkdir home)
    home))

(defn mekit-app-file
  ([app path]
    (let [home (mekit-app-home app)]
      (file-join home path)))
  ([path]
    (let [home (mekit-data-home)]
      (mkdir home)
      (file-join home path))))
